Back to course home
0% completed
Vote For New Content
Oracle Hard
Integer to English Words (hard)
Solution: Integer to English Words
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Merge K Sorted Lists (hard)
Solution: Merge K Sorted Lists
Max Points on a Line (hard)
Solution: Max Points on a Line
Sudoku Solver (hard)
Solution: Sudoku Solver
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Sort Colors (medium)
Problem Statement
Given an array containing 0s, 1s and 2s, sort the array in-place. You should treat numbers of the array as objects, hence, we can’t count 0s, 1s, and 2s to recreate the array.
The flag of the Netherlands consists of three colors: red, white and blue; and since our input array also consists of three different numbers that is why it is called Dutch National Flag problem.
Examples
Example 1
- Input: arr = [1, 0, 2, 1, 0]
- Output: [0, 0, 1, 1, 2]
- Explanation:
- All 0s are moved to the front, 1s in the middle, and 2s at the end.
- The relative order within each group doesn't matter.
Example 2
- Input: arr= [2, 2, 0, 1, 2, 0]
- Output: [0, 0, 1, 2, 2, 2]
- Explanation:
- All 0s come first, followed by the 1, and then all 2s at the end.
- Sorting is done in-place without using extra space or counting.
Constraints:
n == arr.length1 <= n <= 300arr[i] is either 0, 1, or 2.
Try it yourself
Try solving this question here:
Java
. . . .
Solution: Binary Tree Right Side View
Solution: Sort Colors
Mark as Completed
On this page
Problem Statement
Examples
Example 2
Try it yourself